/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.


See LICENSE and AUTHORS for more information.
*/

#include "chatwidget.h"
#include <QDebug>
#include "inputedit.h"
#include <QTimer>

/**
* @brief construct new dockable chatWidget without data
* @param parent optional QDockWiget parent
*/
chatWidget::chatWidget(QDockWidget *parent)
    : QDockWidget(parent)
{
    form = new Ui_chatWidget;
    form->setupUi(this);


    this->buddyName = "";
    this->jid = "";
    this->index = QModelIndex();
    this->accID = -1;
}



/**
* @brief construct new dockable chatWidget with buddy data
* @param accID the account id this chat is associated with
* @param name the chat partner's name
* @param jid the chat partner's jid
*/
chatWidget::chatWidget(int accID,QString name, QString jid)
{
    form = new Ui_chatWidget;
    form->setupUi(this);

    this->buddyName = name;
    this->jid = jid;
    this->index = index;
    this->setWindowTitle(this->buddyName);
    this->accID = accID;


    this->input = new InputEdit(this->form->dockWidgetContents);
    this->input->setObjectName("input");
    this->input->show();
    form->verticalLayout->addWidget(this->input);
    connect(this->input,SIGNAL(prepareMsg()),this,SLOT(prepareMsg()));
    this->stateTimer = new QTimer(this);
    this->stateTimer->setInterval(5000);
    this->isTyping=false;
    this->pausedEmitted=true;
    this->justCleared=false;
    this->stateTimer->start();
    connect(this->input,SIGNAL(cursorPositionChanged ()),this,SLOT(inputChanged()));
    connect(this->stateTimer,SIGNAL(timeout()),this,SLOT(checkTimer()));
}

/**
* @brief set a flag when user types in the input widget
*/
void chatWidget::inputChanged() 
{
  if(this->justCleared)
  {
   this->justCleared = false;
   return;
  }
  if(!this->isTyping && this->pausedEmitted)
    emit stateChanged(this->accID, this->jid, "composing");
  this->isTyping = true;
  this->pausedEmitted = false;
}

/**
* @brief check whether the user typed since last timer tick, emit stateChanged() otherwise
*/
void chatWidget::checkTimer() 
{
  if(this->isTyping)
    this->isTyping = false;
  else 
    if (!this->pausedEmitted)
    {
      this->pausedEmitted = true; 
      emit stateChanged(this->accID, this->jid, "paused");
    } 
}

/**
* @brief grip the user input and initiate the message sending
*
* takes the user input and the accountID and Jid stored in the chatwidget
* to emit the msgSend(int,int,QString) signal which should be connected to
* SslClient and sent to the core there.
*
* Also clears the input-field afterwards.
*/
void chatWidget::prepareMsg()
{
    emit(msgSend(this->accID,this->jid,this->input->toPlainText()));
    this->justCleared=true;
    this->input->clear();
    this->pausedEmitted = true;
    this->isTyping = false;
}

/**
* @brief update the buddy information in this widget
*
* This should get called, whenever the buddy information changes,
* like the buddyname.
*
* @param name the (possibly) new name
* @param jid JID
* @param index the index in the BuddyModel
*/
void chatWidget::updateBuddy(QString name,QString jid,QModelIndex index)
{
    this->buddyName = name;
    this->jid = jid;
    this->index = index;
}

/**
* @brief appends a message received from the core into the chatWidget
*
* So far, this only inputs a simple string into the QTextEdit
* This is supposed to have more complex message handling as soon
* as a better chatEdit was created.
*
* senderJID is used to determine whether the message came from this
* client himself or the buddy.
*
* @param senderJID the sender's jid
* @param msg the content of the message
* @param date the date+time when the message was received
*/
void chatWidget::msgRecv(QString senderJID,QString msg,QDateTime date)
{
    QString sender = "You";
    if (this->jid == senderJID) {
        sender = this->buddyName;
    }

    QString txt = "%1 - %2: %3";
    txt = txt.arg(date.toString("hh:mm:ss")).arg(sender).arg(msg);

    this->form->textEdit->append(txt);
}

/**
* @brief updates the widget with new buddy state
*
* This is chat notification of XEP-0085
* @param state The new state
*/
void chatWidget::stateChanged(QString state)
{
    qDebug() << "state recv in widget: " << state;
    if(state == "composing")
    {
      this->form->notificationLabel->setText("Buddy is typing...");
    }
    else if(state == "paused")
    {
      this->form->notificationLabel->setText("Buddy stopped typing");
    }
    else if(state == "active")
    {
      this->form->notificationLabel->setText("");
    }
}